package com.lottery.material.service.admin.circle.impl;

import com.lottery.material.dao.circle.CircleInfoMapper;
import com.lottery.material.dao.circle.CircleNoticeInfoMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.circle.CircleInfo;
import com.lottery.material.pojo.circle.CircleNoticeInfo;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.circle.CircleNoticeInfoCustom;
import com.lottery.material.service.admin.circle.CircleNoticeInfoService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：圈子公告信息服务接口实现类
 * Created by 朱正磊 on 2019-04-13 15:39
 */
@Slf4j
@Service("adminCircleNoticeInfoService")
public class CircleNoticeInfoServiceImpl implements CircleNoticeInfoService {

    private final CircleNoticeInfoMapper circleNoticeInfoMapper;

    private final CircleInfoMapper circleInfoMapper;

    @Autowired
    public CircleNoticeInfoServiceImpl(CircleNoticeInfoMapper circleNoticeInfoMapper,
                                       CircleInfoMapper circleInfoMapper) {
        this.circleNoticeInfoMapper = circleNoticeInfoMapper;
        this.circleInfoMapper = circleInfoMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param circleNoticeInfo 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(CircleNoticeInfo circleNoticeInfo) {
        circleNoticeInfo.setCreateTime(new Date());//初始化创建时间
        circleNoticeInfo.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue());//默认未逻辑删除
        return circleNoticeInfoMapper.insertSelective(circleNoticeInfo);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return circleNoticeInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public CircleNoticeInfo findById(Integer id) {
        return circleNoticeInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param circleNoticeInfo 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(CircleNoticeInfo circleNoticeInfo) {
        return circleNoticeInfoMapper.updateByPrimaryKeySelective(circleNoticeInfo);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<CircleNoticeInfoCustom> findCustomListPage(Page page) {
        List<CircleNoticeInfoCustom> list = circleNoticeInfoMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public CircleNoticeInfoCustom findCustomById(Integer id) {
        CircleNoticeInfoCustom custom = circleNoticeInfoMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(CircleNoticeInfoCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE, custom.getIsDelete(), CommonEnum.class));
        //圈子名称
        CircleInfo circleInfo = circleInfoMapper.selectByPrimaryKey(custom.getCircleId());
        custom.setCircleName(circleInfo.getCircleName());
    }

    /**
     * description：根据主键删除一条数据（逻辑删除）
     * user 朱正磊
     * time 2019-04-30 11:30
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteLogicById(Integer id) {
        CircleNoticeInfo circleNoticeInfo = new CircleNoticeInfo();
        circleNoticeInfo.setId(id);
        circleNoticeInfo.setIsDelete((Integer) CommonEnum.IS_DELETE_TRUE.getValue());
        Integer flag = circleNoticeInfoMapper.updateByPrimaryKeySelective(circleNoticeInfo);
        log.info("【逻辑删除持久化操作数据库状态：flag = {}】", flag);
        return flag;
    }

    /**
     * description：根据圈子id和是否删除状态查询圈子公告信息列表数据
     * user 朱正磊
     * time 2019-05-28 14:22
     * @param circleId 圈子id
     * @param isDelete 是否删除状态
     * @return 返回圈子公告信息列表数据
     */
    @Override
    public List<CircleNoticeInfo> findByCircleIdAndIsDelete(Integer circleId, Integer isDelete) {
        return circleNoticeInfoMapper.findByCircleIdAndIsDelete(circleId, isDelete);
    }

}
